/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Serum.Form.Module.Fields.RemoteSelect = Ext.extend(Ext.Panel, {

    /**
     * Constructor
     *
     * @param   {Object}    baseConfig
     */
    constructor: function(baseConfig) {
        // name specifies the field's name in the form

        var store = new Ext.data.JsonStore({
            id:             'id',
            root:           'result',
            fields:         [
                                 { name: 'id', mapping: 'id' }, { name: 'name', mapping: 'name' }
                            ],
            autoLoad:       true,
            totalProperty:  'count',
            url:            Serum.Selector.get().requestUrl(),
            remoteSort:     true,
            baseParams:     {
                                method: Serum.Selector.get().method({action: 'listRemoteSelect', controller: baseConfig.controller }),
                                requestId: Serum.Helper.Request.id()
                            }
        });

        this.template = new Ext.form.ComboBox({
                             hiddenName:        'rel-' + baseConfig.model,
                             store:             store,
                             fieldLabel:        __(Serum.Selector.get().module + '~form_label_' + baseConfig.model),
                             displayField:      'name',
                             valueField:        'id',
                             mode:              'remote',
                             forceSelection:    true,
                             triggerAction:     'all',
                             selectOnFocus:     true,
                             value:             baseConfig.value || null,
                             displayValue:      'Toto'
        });

        Serum.Form.Module.Fields.ListFiles.superclass.constructor.call(this, {
            border: false,
            layout: 'form',
            items: [
                this.template
            ]
        });
    },

    /**
     * Validate form
     *
     * @param   {String}    value
     */
    validateForm: function(value) {
        return true;
    }
});

Ext.reg('remote_select', Serum.Form.Module.Fields.RemoteSelect);